home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dirut / dosdir21.zip / MATCH.C < prev    next >
C/C++ Source or Header  |  1994-01-11  |  10KB  |  257 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   match.c
  4.  
  5.   The match() routine recursively compares a string to a "pattern" (regular
  6.   expression), returning TRUE if a match is found or FALSE if not.  This
  7.   version is specifically for use with unzip.c:  as did the previous match()
  8.   routines from SEA and J. Kercheval, it leaves the case (upper, lower, or
  9.   mixed) of the string alone, but converts any uppercase characters in the
  10.   pattern to lowercase if indicated by the global var pInfo->lcflag (which
  11.   is to say, string is assumed to have been converted to lowercase already,
  12.   if such was necessary).
  13.  
  14.   GRR:  reversed order of text, pattern in matche() (now same as match());
  15.         added ignore_case/ic flags, Case() macro.
  16.  
  17.   PK:   replaced matche() with recmatch() from Zip, modified to have an
  18.         ignore_case argument; replaced test frame with simpler one.
  19.  
  20.   ---------------------------------------------------------------------------
  21.  
  22.   Copyright on recmatch() from Zip's util.c (although recmatch() was almost
  23.   certainly written by Mark Adler...ask me how I can tell :-) ):
  24.  
  25.      Copyright (C) 1990-1992 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  26.      Kai Uwe Rommel and Igor Mandrichenko.
  27.  
  28.      Permission is granted to any individual or institution to use, copy,
  29.      or redistribute this software so long as all of the original files are
  30.      included unmodified, that it is not sold for profit, and that this copy-
  31.      right notice is retained.
  32.  
  33.   ---------------------------------------------------------------------------
  34.  
  35.   Match the pattern (wildcard) against the string (fixed):
  36.  
  37.      match(string, pattern, ignore_case);
  38.  
  39.   returns TRUE if string matches pattern, FALSE otherwise.  In the pattern:
  40.  
  41.      `*' matches any sequence of characters (zero or more)
  42.      `?' matches any character
  43.      [SET] matches any character in the specified set,
  44.      [!SET] or [^SET] matches any character not in the specified set.
  45.  
  46.   A set is composed of characters or ranges; a range looks like ``character
  47.   hyphen character'' (as in 0-9 or A-Z).  [0-9a-zA-Z_] is the minimal set of
  48.   characters allowed in the [..] pattern construct.  Other characters are
  49.   allowed (ie. 8 bit characters) if your system will support them.
  50.  
  51.   To suppress the special syntactic significance of any of ``[]*?!^-\'', in-
  52.   side or outside a [..] construct and match the character exactly, precede
  53.   it with a ``\'' (backslash).
  54.  
  55.   Note that "*.*" and "*." are treated specially under MS-DOS if DOSWILD is
  56.   defined.  See the DOSWILD section below for an explanation.
  57.  
  58.   ---------------------------------------------------------------------------*/
  59.  
  60. #include <string.h>
  61. #include "match.h" /* define ToLower() in here (for Unix, define ToLower
  62.                        * to be macro (using isupper()); otherwise just use
  63.                        * tolower() */
  64.  
  65. #define Case(x)  (ic? ToLower(x) : (x))
  66.  
  67. static int recmatch OF((uch *pattern, uch *string, int ignore_case));
  68.  
  69. /* dd_match() is a shell to recmatch() to return only Boolean values. */
  70.  
  71. int dd_match(string, pattern, ignore_case)
  72.     const char *string, *pattern;
  73.     int ignore_case;
  74. {
  75. #if (defined(MSDOS) && defined(DOSWILD))
  76.     char *dospattern;
  77.     int j = strlen(pattern);
  78.  
  79. /*---------------------------------------------------------------------------
  80.     Optional MS-DOS preprocessing section:  compare last three chars of the
  81.     wildcard to "*.*" and translate to "*" if found; else compare the last
  82.     two characters to "*." and, if found, scan the non-wild string for dots.
  83.     If in the latter case a dot is found, return failure; else translate the
  84.     "*." to "*".  In either case, continue with the normal (Unix-like) match
  85.     procedure after translation.  (If not enough memory, default to normal
  86.     match.)  This causes "a*.*" and "a*." to behave as MS-DOS users expect.
  87.   ---------------------------------------------------------------------------*/
  88.  
  89.     if ((dospattern = (char *)malloc(j+1)) != NULL) {
  90.         strcpy(dospattern, pattern);
  91.         if (!strcmp(dospattern+j-3, "*.*")) {
  92.             dospattern[j-2] = '\0';                    /* nuke the ".*" */
  93.         } else if (!strcmp(dospattern+j-2, "*.")) {
  94.             char *p = strchr(string, '.');
  95.  
  96.             if (p) {   /* found a dot:  match fails */
  97.                 free(dospattern);
  98.                 return 0;
  99.             }
  100.             dospattern[j-1] = '\0';                    /* nuke the end "." */
  101.         }
  102.         j = recmatch((uch *)dospattern, (uch *)string, ignore_case);
  103.         free(dospattern);
  104.         return j == 1;
  105.     } else
  106. #endif /* MSDOS && DOSWILD */
  107.     return recmatch((uch *)pattern, (uch *)string, ignore_case) == 1;
  108. }
  109.  
  110.  
  111. static int recmatch(p, s, ic)
  112.     uch *p;               /* sh pattern to match */
  113.     uch *s;               /* string to which to match it */
  114.     int ic;               /* true for case insensitivity */
  115. /* Recursively compare the sh pattern p with the string s and return 1 if
  116.  * they match, and 0 or 2 if they don't or if there is a syntax error in the
  117.  * pattern.  This routine recurses on itself no more deeply than the number
  118.  * of characters in the pattern. */
  119. {
  120.     unsigned int c;       /* pattern char or start of range in [-] loop */ 
  121.  
  122.     /* Get first character, the pattern for new recmatch calls follows */
  123.     c = *p++;
  124.  
  125.     /* If that was the end of the pattern, match if string empty too */
  126.     if (c == 0)
  127.         return *s == 0;
  128.  
  129.     /* '?' (or '%') matches any character (but not an empty string) */
  130. #ifdef VMS
  131.     if (c == '%')         /* GRR:  make this conditional, too? */
  132. #else /* !VMS */
  133.     if (c == '?')
  134. #endif /* ?VMS */
  135.         return *s ? recmatch(p, s + 1, ic) : 0;
  136.  
  137.     /* '*' matches any number of characters, including zero */
  138. #ifdef AMIGA
  139.     if (c == '#' && *p == '?')     /* "#?" is Amiga-ese for "*" */
  140.         c = '*', p++;
  141. #endif /* AMIGA */
  142.     if (c == '*') {
  143.         if (*p == 0)
  144.             return 1;
  145.         for (; *s; s++)
  146.             if ((c = recmatch(p, s, ic)) != 0)
  147.                 return (int)c;
  148.         return 2;       /* 2 means give up--match will return false */
  149.     }
  150.  
  151. #ifndef VMS             /* No bracket matching in VMS */
  152.     /* Parse and process the list of characters and ranges in brackets */
  153.     if (c == '[') {
  154.         int e;          /* flag true if next char to be taken literally */
  155.         uch *q;         /* pointer to end of [-] group */
  156.         int r;          /* flag true to match anything but the range */
  157.  
  158.         if (*s == 0)                           /* need a character to match */
  159.             return 0;
  160.         p += (r = (*p == '!' || *p == '^'));   /* see if reverse */
  161.         for (q = p, e = 0; *q; q++)            /* find closing bracket */
  162.             if (e)
  163.                 e = 0;
  164.             else
  165.                 if (*q == '\\')      /* GRR:  change to ^ for MS-DOS, OS/2? */
  166.                     e = 1;
  167.                 else if (*q == ']')
  168.                     break;
  169.         if (*q != ']')               /* nothing matches if bad syntax */
  170.             return 0;
  171.         for (c = 0, e = *p == '-'; p < q; p++) {  /* go through the list */
  172.             if (e == 0 && *p == '\\')             /* set escape flag if \ */
  173.                 e = 1;
  174.             else if (e == 0 && *p == '-')         /* set start of range if - */
  175.                 c = *(p-1);
  176.             else {
  177.                 unsigned int cc = Case(*s);
  178.  
  179.                 if (*(p+1) != '-')
  180.                     for (c = c ? c : *p; c <= *p; c++)  /* compare range */
  181.                         if (Case(c) == cc)
  182.                             return r ? 0 : recmatch(q + 1, s + 1, ic);
  183.                 c = e = 0;   /* clear range, escape flags */
  184.             }
  185.         }
  186.         return r ? recmatch(q + 1, s + 1, ic) : 0;  /* bracket match failed */
  187.     }
  188. #endif /* !VMS */
  189.  
  190.     /* if escape ('\'), just compare next character */
  191.     if (c == '\\' && (c = *p++) == 0)     /* if \ at end, then syntax error */
  192.         return 0;
  193.  
  194.     /* just a